package com.minimalist.basic.controller.basic;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.minimalist.basic.service.PermService;
import com.minimalist.common.entity.basic.vo.perm.PermQueryVO;
import com.minimalist.common.entity.basic.vo.perm.PermVO;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 11987
 */
@Validated
@RestController
@RequestMapping("/basic/permission")
@Tag(name = "权限管理")
public class PermController{

    @Autowired
    private PermService permService;

    @PostMapping("/addPerm")
    @SaCheckPermission("basic:perm:add")
    @Operation(summary = "添加权限")
    public R<?> addPerm(
            @RequestBody @Validated(Add.class) PermVO permVO){
        permService.addPerm(permVO);
        return R.ok();
    }

    @DeleteMapping("/deletePermByPermId")
    @SaCheckPermission("basic:perm:delete")
    @Operation(summary = "删除权限 -> 根据权限ID删除")
    public R<?> deletePermByPermId(
            @RequestParam("permId") @NotNull(message = "权限ID不能为空")
            @Parameter(name = "permId", required = true, description = "权限ID")
            Long permId){
        permService.deletePermByPermId(permId);
        return R.ok();
    }

    @PutMapping("/updatePermByPermId")
    @SaCheckPermission("basic:perm:update")
    @Operation(summary = "修改权限 -> 根据权限ID修改")
    public R<?> updatePermByPermId(
            @RequestBody @Validated(Update.class) PermVO permVO){
        permService.updatePermByPermId(permVO);
        return R.ok();
    }

    @GetMapping("/getPermList")
    @SaCheckPermission("basic:perm:get")
    @Operation(summary = "查询权限列表(不分页，获取全部数据) -> 权限管理使用")
    public R<List<PermVO>> getPermList(PermQueryVO queryVO){
        return R.ok(permService.getPermList(queryVO));
    }

    @GetMapping("/getEnablePermList")
    @Operation(summary = "查询系统租户权限列表 -> 只获取正常状态的权限")
    public R<List<PermVO>> getEnablePermList(){
        return R.ok(permService.getEnablePermList());
    }

    @GetMapping("/getTenantEnablePermList")
    @Operation(summary = "查询租户权限列表 -> 只获取正常状态的权限")
    public R<List<PermVO>> getTenantEnablePermList(){
        return R.ok(permService.getTenantEnablePermList());
    }

    @GetMapping("/getPermByPermId/{permId}")
    @SaCheckPermission("basic:perm:get")
    @Operation(summary = "根据权限ID查询权限")
    public R<PermVO> getPermByPermId(
            @PathVariable(value = "permId")
            @NotNull(message = "权限ID不能为空")
            @Parameter(name = "permId", description = "权限ID", required = true)
            Long permId){
        return R.ok(permService.getPermByPermId(permId));
    }

}
